package com.ybb.servlet;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.ybb.dao.ProviderDao;
import com.ybb.dao.ProviderDaoImpl;
import com.ybb.order_enum.Operator;
import com.ybb.pojo.Provider;
import com.ybb.pojo.User;
import com.ybb.service.ProviderService;
import com.ybb.service.ProviderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/jsp/provider.do")
public class ProviderServlet extends HttpServlet {

    private ProviderService providerService = new ProviderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (Operator.QUERY.getOperator().equals(method)) {
            getAllProviders(req, resp);
        } else if (Operator.VIEW.getOperator().equals(method)) {
            getProviderByUid(req, resp);
        } else if (Operator.MODIFY.getOperator().equals(method)) {
            modifyProvider(req, resp);
        }else if (Operator.DELPROVIDER.getOperator().equals(method)) {
            deleteProvider(req, resp);
        }

    }

    private void deleteProvider(HttpServletRequest req, HttpServletResponse resp) {

        String proid = req.getParameter("proid");
        Map<String, String> map = new HashMap<String, String>();
        boolean exists = providerService.isExist(Long.valueOf(proid));
        if (!exists) {
            map.put("delResult", "notExist");
        } else {

            int deleteRows = providerService.deleteProvider(Long.valueOf(proid));

            if (deleteRows > 0) {
                map.put("delResult", "true");
            } else {
                map.put("delResult", "false");
            }
        }
        resp.setContentType("application/json");
        String jsonString = JSONArray.toJSONString(map);
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            writer.write(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.flush();
            writer.close();
        }

    }

    private void modifyProvider(HttpServletRequest req, HttpServletResponse resp) {
        String providerId = req.getParameter("proid");
        Provider provider = providerService.getProviderByUid(Long.valueOf(providerId));
        req.getSession().setAttribute("provider", provider);
        try {
            req.getRequestDispatcher("providermodify.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void getProviderByUid(HttpServletRequest req, HttpServletResponse resp) {

        String userId = req.getParameter("proid");
        Provider provider = providerService.getProviderByUid(Long.valueOf(userId));
        req.getSession().setAttribute("provider", provider);
        try {
            req.getRequestDispatcher("providerview.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void getAllProviders(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();

        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");

        session.setAttribute("queryProCode", queryProCode);
        session.setAttribute("queryProName", queryProName);

        List<Provider> providerList = providerService.getProviderList(queryProCode, queryProName);

        req.setAttribute("providerList", providerList);

        try {
            req.getRequestDispatcher("providerlist.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (Operator.MODIFYEXE.getOperator().equals(method)) {
            providerModify(req, resp);

        }else if(Operator.ADD.getOperator().equals(method)){
            addProvider(req,resp);
        }else if(Operator.MODIFY.getOperator().equals(method)){
            providerModify(req, resp);
        }
    }

    private void addProvider(HttpServletRequest req, HttpServletResponse resp) {
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proFax = req.getParameter("proFax");

        Provider provider=new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProFax(proFax);

        User user  = (User) req.getSession().getAttribute("userSession");
        provider.setCreateBy(user.getId());
        provider. setCreationDate(new Date());


        int insertRows = providerService.insertProvider(provider);
        if(insertRows>0){
            try {
                req.getRequestDispatcher("insert-provider-success.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            req.getSession().setAttribute("insertFailed","插入供应商信息失败");
            try {
                req.getRequestDispatcher("provideradd.jsp").forward(req,resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void providerModify(HttpServletRequest req, HttpServletResponse resp) {

        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proFax = req.getParameter("proFax");
        String proid = req.getParameter("proid");


         Provider provider=new Provider();
        provider.setId(Long.valueOf(proid));
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProFax(proFax);


        int upDateRows = providerService.upDateProvider(provider);
        if (upDateRows > 0) {
            try {
                req.getRequestDispatcher("provider_modify_success.jsp").forward(req, resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            req.getSession().setAttribute("updateProviderFailed", "修改供应商信息失败");
            try {
                req.getRequestDispatcher("providermodify.jsp").forward(req, resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}